package com.xd.demo.util;

import com.alibaba.fastjson.JSON;
import com.xd.demo.dto.JdCreditApplyReqDto;
import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;

public class AesUtils {
    private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};


    public static String encryptAES(String encryptKey, String plainTextData) throws Exception {
        Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
        byte[] raw = encryptKey.getBytes("utf-8");
        IvParameterSpec iv = new IvParameterSpec(raw);
        cipher.init( Cipher.ENCRYPT_MODE, new SecretKeySpec( raw, "AES" ),iv);
        byte[] encryptedData = cipher.doFinal( plainTextData.getBytes( "UTF-8" ) );
        return Base64.encodeBase64String( encryptedData );
    }

    public static String decryptAES(String encryptKey, String encryptedData) throws Exception {
        Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
        byte[] raw = encryptKey.getBytes("utf-8");
        IvParameterSpec iv = new IvParameterSpec(raw);
        cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( raw, "AES" ), iv );
        byte[] decryptedData = cipher.doFinal( Base64.decodeBase64( encryptedData ) );
        return new String( decryptedData, "UTF-8" );
    }

    public static byte[] decryptAESByte(String encryptKey, String fileData) throws Exception {
        Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
        byte[] raw = encryptKey.getBytes("utf-8");
        IvParameterSpec iv = new IvParameterSpec(raw);
        cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( raw, "AES" ), iv );
        return cipher.doFinal( Base64.decodeBase64(fileData) );
    }

    public static String sign(String data, String salt) throws Exception {
        data = data + "|" + salt;
        byte[] bytes = MessageDigest.getInstance( "MD5" ).digest( data.getBytes( "UTF-8" ) );
        return byteToString( bytes );
    }

    private static String byteToString(byte[] bByte) {
        StringBuilder sb = new StringBuilder();
        for (byte aBByte : bByte) {
            sb.append(byteToArrayString(aBByte));
        }
        return sb.toString();
    }

    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return HEX_DIGITS[iD1] + HEX_DIGITS[iD2];
    }


    public static void main(String[] args) throws Exception {

//        String data = "{\n" +
//                "\t\"applyNo\": \"1086143071285944321\",\n" +
//                "\t\"applyTime\": \"20190118140812\",\n" +
//                "\t\"businessType\": \"JT\",\n" +
//                "\t\"riskInfo\": {\n" +
//                "\t\t\"addressData\": {\n" +
//                "\t\t\t\"addressCredibility\": \"-1\",\n" +
//                "\t\t\t\"addressUsedExist\": \"0\",\n" +
//                "\t\t\t\"addressUsedTime\": \"-1\"\n" +
//                "\t\t},\n" +
//                "\t\t\"blackData\": {\n" +
//                "\t\t\t\"blacklistExist\": \"1\",\n" +
//                "\t\t\t\"blacklistScore\": \"1\",\n" +
//                "\t\t\t\"blacklistSource\": \"2,1\"\n" +
//                "\t\t},\n" +
//                "\t\t\"multiData\": {\n" +
//                "\t\t\t\"queryCount\": \"18\",\n" +
//                "\t\t\t\"queryCount14\": \"0\",\n" +
//                "\t\t\t\"queryCount180\": \"0\",\n" +
//                "\t\t\t\"queryCount30\": \"0\",\n" +
//                "\t\t\t\"queryCount60\": \"0\",\n" +
//                "\t\t\t\"queryCount7\": \"0\",\n" +
//                "\t\t\t\"queryCount90\": \"0\"\n" +
//                "\t\t},\n" +
//                "\t\t\"riskData\": {\n" +
//                "\t\t\t\"accountUsageTimeLv\": \"5\",\n" +
//                "\t\t\t\"addressStability\": \"0\",\n" +
//                "\t\t\t\"carLevel\": \"-1\",\n" +
//                "\t\t\t\"careerForecast\": \"2\",\n" +
//                "\t\t\t\"creditUsageAmount180\": \"1\",\n" +
//                "\t\t\t\"creditUsageCount180\": \"1\",\n" +
//                "\t\t\t\"creditUsageTimeLv\": \"4\",\n" +
//                "\t\t\t\"fraudScore\": \"0.0595\",\n" +
//                "\t\t\t\"houseLevel\": \"5\",\n" +
//                "\t\t\t\"incomeLevel\": \"4\",\n" +
//                "\t\t\t\"installmentAmount180\": \"1\",\n" +
//                "\t\t\t\"installmentCount180\": \"1\",\n" +
//                "\t\t\t\"mobileStability\": \"2\",\n" +
//                "\t\t\t\"onlineAmountLv\": \"2\",\n" +
//                "\t\t\t\"onlineAmountMax\": \"-1\"\n" +
//                "\t\t}\n" +
//                "\t},\n" +
//                "\t\"userInfo\": {\n" +
//                "\t\t\"activateTag\": \"UN_ACTIVATED\",\n" +
//                "\t\t\"address\": \"北京朝阳区三环以内hhhh\",\n" +
//                "\t\t\"applyAmount\": \"10000\",\n" +
//                "\t\t\"certNo\": \"422802198909120323\",\n" +
//                "\t\t\"certType\": \"10100\",\n" +
//                "\t\t\"pin\": \"jd_5eae88eb7f43f\",\n" +
//                "\t\t\"userTel\": \"18210939931\",\n" +
//                "\t\t\"cardNo\": \"123123123\",\n" +
//                "\t\t\"bankNo\": \"123123123\",\n" +
//                "\t\t\"bkTel\": \"123123123\",\n" +
//                "\t\t\"idExpire\": \"20191010\",\n" +
//                "\t\t\"areaCode\": \"520000\",\n" +
//                "\t\t\"shopPhone\": \"123123\"\n" +
//                "\t}\n" +
//                "}";
//        JdCreditApplyReqDto object = JSON.parseObject(data, JdCreditApplyReqDto.class);
//        String s = JSON.toJSONString(object);
//        System.out.println(s);。


        FileInputStream fileInputStream = new FileInputStream(new File("C:\\Users\\xiedi\\Desktop\\aa\\JD2020091401.tar.gz"));

    }
}
